<?php

namespace app\admin\controller\common;

use app\common\controller\AdminBase;
use app\common\helper\NetPay;
use app\common\logic\common\CommonLogic;
use app\common\model\GroupOrder;
use app\common\model\Refund;

class Pay extends AdminBase
{
    public function refund()
    {
        $order = GroupOrder::where("id",$this->data['id'])->find();
        if (!$order || $order->status < 1) {
            $this->error('订单错误');
        }
        if ($order->is_refunded == 1) $this->error('订单已经退款');
        $refund_trade_no = CommonLogic::makeOrderNo();
        $pay = \app\common\model\Pay::where("order_id",$this->data['id'])->find();
        Refund::create([
            "order_id" => $this->data['id'],
            "refund_trade_no" => $refund_trade_no,
            "out_trade_no" => $pay->out_trade_no,
            "money" => $pay->money,
            "user_id" => $pay->user_id
        ]);
        $payObj = new NetPay();
        $status = $payObj->createWxRefund($refund_trade_no,$pay->out_trade_no,$pay->money,$pay->money );
        if ($status == "SUCCESS") {
            $this->success();
        } else {
            $this->error('退款失败');
        }
    }
}